$(document).ready(function(){    
    
    
    
    $(".currency").each(function() {
        $(this).formatCurrency();
    });
});



function openGraph(creditId)
{
    $.ajax({
            type: 'post',
            url: '/payment/getSchedule/',
            data: {
                creditId : creditId
            },
            beforeSend: function(){
                $.blockUI({ message: null });
                $("#graphicPopupTbody").html('<tr><td colspan="7">Уншиж байна...</td></tr>');
            },
            dataType: "json",
            success: function (result) {
                
                if(result.status === 'success')
                {
                    var html = "";
                    var counter = 1;
                    $(result.data.CREDIT_SCHEDULE).each(function(key, value){
                        
                        html += "<tr>\n\
                                    <td>"+counter+"</td>\n\
                                    <td>"+value.TARGET_DATE+"</td>\n\
                                    <td>"+value.DAYS+"</td>\n\
                                    <td class=\"currency\">"+value.MAIN_AMOUNT+"</td>\n\
                                    <td class=\"currency\">"+value.INTEREST_AMOUNT+"</td>\n\
                                    <td class=\"currency\">"+value.TOTAL_AMOUNT+"</td>\n\
                                    <td class=\"currency\">"+value.BALANCE+"</td>\n\
                                </tr>";
                    counter = counter + 1;        
                    });
                    
                    $("#graphicPopupTbody").html(html);
                    $(".currency").formatCurrency();
                }else{
                    $("#graphicPopupTbody").html('<tr><td colspan="7">Хуваарийн мэдээлэл байхгүй.</td></tr>');
                }
            },
            error: function () {
                $.unblockUI();
                $("#graphicPopupContent").html('Зээлийн график олдсонгүй!');
            }
        }).done(function(){
            $.unblockUI();
        });
    
}

function openPayment(creditId)
{
    $.ajax({
            type: 'post',
            url: '/payment/getPayment/',
            data: {
                creditId : creditId
            },
            beforeSend: function(){
                $.blockUI({ message: null });
                $("#paymentPopupTbody").html('<tr><td colspan="9">Уншиж байна...</td></tr>');
            },
            dataType: "json",
            success: function (result) {
                
                if(result.status === 'success')
                {
                    var html = "";
                    var counter = 1;
                    $(result.data).each(function(key, value){
                        
                        html += "<tr>\n\
                                    <td>"+counter+"</td>\n\
                                    <td>"+value.TARGET_DATE+"</td>\n\
                                    <td>"+value.DAYS+"</td>\n\
                                    <td class=\"currency\">"+value.MAIN_AMOUNT+"</td>\n\
                                    <td class=\"currency\">"+value.INTEREST_AMOUNT+"</td>\n\
                                    <td class=\"currency\">"+value.TOTAL_AMOUNT+"</td>\n\
                                    <td class=\"currency\">"+value.PAID_AMOUNT+"</td>\n\
                                    <td>"+value.NAME+"</td>\n\
                                    <td class=\"currency\">"+value.BALANCE+"</td>\n\
                                </tr>";
                    counter = counter + 1;        
                    });
                    
                    $("#paymentPopupTbody").html(html);                    
                    $(".currency").formatCurrency();
                }else{
                    $("#paymentPopupTbody").html('<tr><td colspan="9">Төлөлтийн мэдээлэл байхгүй.</td></tr>');
                }
            },
            error: function () {
                $.unblockUI();
                $("#paymentPopupContent").html('Зээлийн график олдсонгүй!');
            }
        }).done(function(){
            $.unblockUI();
        });
    
}

function phoneHistory()
{
    
}

function emailHistory()
{
    
}

function addressHistory()
{
    if(!$('#historyAddressBtn').hasClass('opened')){
        var html = '<div id="addressHistoryContainer" class="addressHistoryContainer" style="width: 400px;"><table class="table"><thead><tr><th class="col-md-1">№</th><th class="col-md-7">Хаяг</th><th class="col-md-4">Өөрчлөгдсөн огноо</th></tr></thead><tbody><tr><td></td><td></td><td></td></tr></tbody></table></div>';
        $('#historyAddressBtn').after(html);
        $('#addressHistoryContainer').slideDown();
        $('#historyAddressBtn').addClass('opened');
    }else{
        $('#addressHistoryContainer').slideUp();
        $('#historyAddressBtn').removeClass('opened');
    }
}